package com.yami.shop.live.common.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.enums.LiveInterfaceType;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.wx.WxInterfaceUtil;
import com.yami.shop.config.WxConfig;
import com.yami.shop.live.common.dao.LiveRoomProdMapper;
import com.yami.shop.live.common.model.LiveProdStore;
import com.yami.shop.live.common.model.LiveRoom;
import com.yami.shop.live.common.model.LiveRoomProd;
import com.yami.shop.live.common.service.LiveLogService;
import com.yami.shop.live.common.service.LiveRoomProdService;
import com.yami.shop.live.common.service.LiveRoomService;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 *
 *
 * @author lhd
 * @date 2020-08-05 08:53:17
 */
@Service
@AllArgsConstructor
public class LiveRoomProdServiceImpl extends ServiceImpl<LiveRoomProdMapper, LiveRoomProd> implements LiveRoomProdService {

    private final LiveRoomProdMapper liveRoomProdMapper;
    private final LiveLogService liveLogService;
    private final WxInterfaceUtil wxInterfaceUtil;
    private final WxConfig wxConfig;
    private final LiveRoomService liveRoomService;

    /**
     * 批量保存直播间和商品的关系
     * @param liveRoom 直播间信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveBatchAndRoomId(LiveRoom liveRoom) throws WxErrorException {
        // 1.校验今日可用次数并保存or修改商家次数记录
        liveLogService.checkNumsAndSaveLog(liveRoom.getShopId(), LiveInterfaceType.IMPORT_ROOM_PROD, "导入直播间商品");

        List<LiveProdStore> liveProdStores = liveRoom.getLiveProdStores();
        if(CollectionUtils.isEmpty(liveProdStores)){
            // 请选择商品
            throw new YamiShopBindException("yami.score.select.num");
        }
        // 2.保存直播间商品关联表
        List<LiveRoomProd> liveRoomProds = new ArrayList<>();
        List<Integer> liveProdIds = new ArrayList<>();
        for (LiveProdStore liveProdStore : liveProdStores) {
            LiveRoomProd liveRoomProd = new LiveRoomProd();
            liveRoomProd.setRoomId(liveRoom.getId());
            liveRoomProd.setProdStoreId(liveProdStore.getLiveProdStoreId());
            liveRoomProds.add(liveRoomProd);
            liveProdIds.add(liveProdStore.getGoodsId());
        }
        saveBatch(liveRoomProds);

        // 3.获取直播间信息
        LiveRoom liveRoomDb = liveRoomService.getById(liveRoom.getId());
        // 4.导入商品
        boolean addStatus = false;
        try {
            addStatus = wxConfig.getWxMaService().getLiveService().addGoodsToRoom(liveRoomDb.getRoomId(), liveProdIds);
        } catch (WxErrorException e) {
            e.printStackTrace();
            log.error("添加微信直播间商品失败");
            log.error(e.getMessage());
            if(e.getError().getErrorCode() == 300022){
                // 300022：此房间号不存在
                throw new YamiShopBindException("yami.live.room.wechat.not.exist");
            }else if(e.getError().getErrorCode() == 300024){
                // 300024：商品不存在
                throw new YamiShopBindException("yami.live.prod.wechat.not.exist");
            }
            throw new YamiShopBindException("yami.add.live.prod.num");

        }
        if(!addStatus){
            // 添加微信直播间商品失败
            throw new YamiShopBindException("yami.add.live.prod.num");
        }
    }
}
